package com.qf.agro.service.device.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.agro.mapper.device.SmartDeviceTypeMapper;
import com.qf.agro.pojo.device.SmartDeviceType;
import com.qf.agro.service.device.SmartDeviceTypeService;
import com.qf.agro.vo.OptionVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringSummary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @company：千锋教育
 * @author：zhy
 * @date：2023/7/2
 */
@Service
public class SmartDeviceTypeServiceImpl implements SmartDeviceTypeService {

    @Resource
    private SmartDeviceTypeMapper smartDeviceTypeMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public Page<SmartDeviceType> findPage(Integer pageNum, Integer pageSize, String name) {
        //1. 创建查询条件对象
        LambdaQueryWrapper<SmartDeviceType> wrapper = new LambdaQueryWrapper<>();
        //2.设置查询条件
        wrapper.like(StringUtils.isNotEmpty(name), SmartDeviceType::getTypeName, name);//where name like %#{name}%
        wrapper.orderByDesc(SmartDeviceType::getId);
        //3 设置分页条件
        Page<SmartDeviceType> page = new Page<>(pageNum, pageSize);
        //4 查询
        page = smartDeviceTypeMapper.selectPage(page, wrapper);
        //5 返回
        return page;
    }

    @Override
    public void save(SmartDeviceType smartDeviceType) {
        smartDeviceTypeMapper.insert(smartDeviceType);
        redisTemplate.delete("sdList");//新增操作后删除redis中的数据
    }

    @Override
    public void update(SmartDeviceType smartDeviceType) {
        smartDeviceTypeMapper.updateById(smartDeviceType);
        redisTemplate.delete("sdList");//修改操作后删除redis中的数据
    }

    @Override
    public void delete(Long id) {
        smartDeviceTypeMapper.deleteById(id);
        redisTemplate.delete("sdList");//删除操作后删除redis中的数据
    }

    @Override
    public SmartDeviceType findById(Long id) {
        return smartDeviceTypeMapper.selectById(id);
    }

    @Override
    public List<OptionVo> selectDeviceOptions() {
        ListOperations<String,OptionVo> ops = redisTemplate.opsForList();
        List<OptionVo> sdList = ops.range("sdList", 0, -1);
        if (sdList.isEmpty()){
            System.out.println("======================redis中没有,去数据库中查询");
            List<OptionVo> optionVos = smartDeviceTypeMapper.selectOptions();
            ops.leftPushAll("sdList",optionVos);//把数据库中查出来的数据存入redis中
            return optionVos;
        }else{
            System.out.println("=======================redis中有,把数据返回");
            List<OptionVo> optionVos = new ArrayList<>(sdList);
            Collections.reverse(optionVos);
            return optionVos;
        }
    }
}
